package com.regan.erp.util;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.jfinal.kit.Prop;

import java.io.File;

/**
 * 邮件发送<br>
 * 借助hutool -> 邮件工具-MailUtil
 *
 * @author zhangby
 * @date 2018/10/10 下午3:47
 */
public class SendEmailUtil {
    private static MailAccount account = null;

    //初始化邮箱账户
    private static MailAccount getAccount() {
        if (account == null) {
            Prop prop = ConfigUtil.emailConfig();
            account = new MailAccount();
            account.setHost(prop.get("email_host"));
            account.setPort(prop.getInt("email_port"));
            account.setAuth(prop.getBoolean("email_auth"));
            account.setFrom(prop.get("email_from"));
            account.setUser(prop.get("email_user"));
            account.setPass(prop.get("email_pass"));
        }
        return account;
    }

    /**
     * 发送邮件给多人
     *
     * @param to 收件人，多个收件人逗号或者分号隔开
     * @param subject 标题
     * @param content 正文
     * @param files 附件列表
     */
    public static void send(String to, String subject, String content, File... files) {
        MailUtil.send(getAccount(),to,subject,content,true,files);
    }
}
